/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// ScriptExec.cpp : Defines the entry point for the console application.
//

#include "ScriptDLL.h"
#include "windowsfunctions.h"

#include <crtdbg.h>


//#define __USE_LEAK_DETECT__
//#ifdef __USE_LEAK_DETECT__
//#include "C:\Projects\3rdPartyTools\TestMemLeakConsoleApp\MemCheckLib\MemLeakDetect.h"
//CMemLeakDetect aDetect;
//#endif

//#include "..\StdFuncs\StandardFunctions.h"

int LoadDLLs(CEngine* theEngine, const char* ExternalFile);

int main(int argc, char* argv[])
{

	//_CrtSetBreakAlloc(148);
	//_CrtSetBreakAlloc(58);

	int ctr;

	String FileName;
	String RedirName;
	String Temp;

	std_stream astream;
	std_ofile_stream ostream;

  SSI_BOOL doFile = 0;
	SSI_BOOL doDEBUG = 0;
	SSI_BOOL bREDIRECT = 0;

	for(ctr = 1; ctr < argc; ctr++){
    Temp = argv[ctr];
		if(Temp == "-d"){
			doDEBUG = 1;
			
		}else if (Temp == "-o"){
			ctr++;
			if(ctr > argc){
				printf("Expect output file name.\n");
				return 0;
			}
			RedirName = argv[ctr];
			bREDIRECT = 1;
		}else{
			FileName = argv[ctr];
			doFile = 1;
		}
    
	}

	printf("FileName is: %s\n", FileName.Str());

  printf("    -----------------------------------------\n");
  printf("    | ArithmeScript Pre-Beta                |\n");
  printf("    | John C. Van Arsdall                   |\n");
	printf("    | http://www.sagittasolutions.com       |\n");
  printf("    | Type 'stop' to quit.                  |\n");
	printf("    |       THIS PUPPY IS A BEAST!!!!       |\n");
  printf("    -----------------------------------------\n");
  
  if(doFile)
  {
		ssi_stream* instream;
		ssi_stream* outstream;
		if(bREDIRECT){
			CStatus aStat;
			aStat = ostream.Open(RedirName, String("w"));
			if(!aStat){
				printf("%s\n", aStat.GetStatusMessage().Str());
				return 0;
			}
			instream = &astream;
			outstream = &ostream;
		}else{
			instream = &astream;
			outstream = &astream;

			SSI_INT64 dPos = FileName.ReverseFind(".");
			String OutName = FileName.SubString(0,dPos-1);
			OutName = OutName + ".log";
			astream.SetOutFile(OutName.Str());
		}
		CScript aScript(instream, outstream);
    aScript.Load(FileName);
		LoadDLLs(aScript.Engine, "Externals.dat");
		if(doDEBUG){
			aScript.Run(DEBUG_MODE);
		}else{
			aScript.Run(NORMAL_MODE);
		}

		if(bREDIRECT){
			ostream.Close();
		}
  }
  else
  {
    CConsole aConsole(&astream, &astream);
		LoadDLLs(aConsole.Engine, "Externals.dat");
    aConsole.Loop();
  }

	Factory_ClearData();
  return 0;
}

int LoadDLLs(CEngine* theEngine, const char* ExternalFile){
	File aFile;
	String aStr;

	if(!aFile.Open(ExternalFile, "r")){
		printf("Warning: %s (external DLL list) not found\n", ExternalFile);
		//String s;
		//s.Format("Warning: %s (external DLL list) not found", ExternalFile);
		//theEngine->GetOutStream()->Disp(s.Str(), 1);
		return 0;
	}

	while(aFile.ReadLine(aStr) >= 0){

		theEngine->SetPackageName(aStr.Str());
		LoadDLL(aStr.Str(), theEngine);
	}
	return 1;

}
